//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	WebEntityMapperExts.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Linq;
using Microsoft.SharePoint;

namespace Smart.Common.Entities
{
	public static class WebEntityMapperExts
	{
		#region LookupValue, UserValue, LinkValue

		/// <summary>
		/// Convert SPFieldLookupValue to LookupValue
		/// </summary>
		public static LookupValue CvtL(this SPFieldLookupValue value)
		{
			if (value == null || value.LookupId <= 0)
				return new LookupValue(Identifier.Empty);

			return new LookupValue(value.LookupId, value.LookupValue);
		}

		/// <summary>
		/// Convert SPFieldLookupValueCollection to LookupValueCollection
		/// </summary>
		public static LookupValueCollection CvtL(this SPFieldLookupValueCollection value)
		{
			if (value == null)
				return new LookupValueCollection();

			return new LookupValueCollection(value.Where(s => s != null && s.LookupId > 0).Select(s => s.CvtL()));
		}

		/// <summary>
		/// Convert SPFieldLookupValue to UserValue
		/// </summary>
		public static UserValue CvtU(this SPFieldLookupValue value)
		{
			if (value == null || value.LookupId <= 0)
				return new UserValue(Identifier.Empty);

			return new UserValue(value.LookupId, value.LookupValue);
		}

		/// <summary>
		/// Convert SPFieldLookupValueCollection to UserValueCollection
		/// </summary>
		public static UserValueCollection CvtU(this SPFieldLookupValueCollection value)
		{
			if (value == null || value.Count == 0)
				return new UserValueCollection();

			return new UserValueCollection(value.Where(s => s != null && s.LookupId > 0).Select(s => s.CvtU()));
		}

		/// <summary>
		/// Convert SPFieldUserValue to UserValue
		/// </summary>
		public static UserValue CvtU(this SPFieldUserValue value)
		{
			if (value == null || value.LookupId <= 0)
				return new UserValue(Identifier.Empty);

			return new UserValue(value.LookupId, value.LookupValue);
		}

		/// <summary>
		/// Convert SPFieldUserValueCollection to UserValueCollection
		/// </summary>
		public static UserValueCollection CvtU(this SPFieldUserValueCollection value)
		{
			if (value == null || value.Count == 0)
				return new UserValueCollection();

			return new UserValueCollection(value.Where(s => s != null && s.LookupId > 0).Select(s => s.CvtU()));
		}

		/// <summary>
		/// Convert SPFieldUrlValue to LinkValue
		/// </summary>
		public static LinkValue CvtL(this SPFieldUrlValue value)
		{
			if (value == null || string.IsNullOrEmpty(value.Url))
				return new LinkValue(null, null);

			return new LinkValue(value.Url, value.Description);
		}

		/// <summary>
		/// Convert SPFieldMultiChoiceValue to ChoiceValueCollection
		/// </summary>
		public static ChoiceValueCollection CvtC(this SPFieldMultiChoiceValue value)
		{
			if (value == null || value.Count == 0)
				return new ChoiceValueCollection();

			var res = new ChoiceValueCollection();

			for (var I = 0; I < value.Count; I++)
			{
				var sv = value[I];

				if (!sv.IsNullOrEmpty())
					res.Add(sv);
			}

			return res;
		}

		#endregion
	}
}